package com.ld.security.common.core.base.column;

import java.util.*;

/**
 * jsons
 */
public class ListJson extends ArrayList implements SuperColumn{

    public static final String keyName="prop";



    public  static final  String valueName="value";




    public ListJson() {
    }



    public ListJson put(Object... vs){
        TreeMap map =   new TreeMap<>();
        for(int i = 0;i<vs.length;i+=2){
            map.put(vs[i],vs[i+1]);
        }
        this.add(map);
        return this;
    }





  public Map toMap() {
     return toMap(keyName,valueName);
  }

        public Map toMap(String idKey,String valueKey){
        HashMap args = new HashMap<>();

        for (Object o : this) {
            if(o instanceof Map){
                Map map = (Map)o;
                Object key =  map.get(idKey);
                Object val = map.get(valueKey);
                if(key != null){
                    if(val.equals(""+true)){
                        val = true;
                    }else  if(val.equals(""+false)){
                        val = false;
                    }
                    args.put(key,val);
                }

            }
        }
        return args;
    }

    /**
     * 判断元素是不是一维集合
     * @return
     */
    public boolean isOneList(){
        if(get(0) instanceof Collection){
            return false;
        }
        return true;
    }


    /**
     * 转化为一维集合
     * @return
     */
    public ListJson toOneList() {
        TreeSet set =  new TreeSet<>();
        itemToSet(this,set);
        ListJson listJson = new ListJson();
        listJson.addAll(set);
        return listJson;
  }

        private static void itemToSet(Collection roots,Set set){
        for (Object root : roots) {
          if(root instanceof  Collection){
              Collection cs =(Collection) root;
              itemToSet(cs,set);
          }else{
              set.add(root);
          }
        }
    }

}
